<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>登记入院</title>
    <link rel="stylesheet" href="css/bootstrap.css">
    <script src="js/jquery.js"></script>
    <script src="js/common.js" type="text/javascript"></script>
    <script src="js/jsrender.js"></script>
    <script src="js/bootstrap.js"></script>
    <script type="text/javascript">
        $(function () {
            if(localStorage.getItem("token")==null){
                window.location.href="login.html"
            }
            
            var today = new Date();
            var nowDate= today.getFullYear() +
                "-" + (today.getMonth()+1<10?"0"+(today.getMonth()+1):(today.getMonth()+1)) +
                "-" + (today.getDate()<10?"0"+today.getDate():today.getDate());
            $("#liveindate").val(nowDate)
            //注册转换器
            $.views.converters("dateformat" , function (val) {
                var mydate = new Date(val)
                var y = mydate.getFullYear()
                var m = mydate.getMonth()+1
                var d = mydate.getDate()
                return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d)
            })
            $("#livein").submit(function(){
                $.ajax({
                    url:GC.bctx + "customers/livein",
                    type:"POST",
                    data:{
                    	customerid:$("#customerid").val(),
                    	bedid:$("#bedid").val(),
                    	liveindate:$("#liveindate").val(),
                    	expectedliveoutdate:$("#expectedliveoutdate").val(),
                    	bookingprepayamount:$("#bookingprepayamount").val()
                    },
                    headers: {"Authorization": localStorage.getItem("token")},
                    success:function(r){
                        if(r.successed){
                            //新增成功，转向显示入住记录页面
                            alert(r.message)
                            window.location.href="listLivein.html"
                        }else{
                            //新增失败，显示失败信息
                            alert(r.message)
                        }

                    }
                })
                return false
            })

            $("#checkCustomer").click(function(){
                $.ajax({
                    url:GC.bctx + "customers/checkCustomer",
                    type:"Get",
                    data:{customerid:$("#customerid").val()},
                    headers: {"Authorization": localStorage.getItem("token")},
                    success:function(r){
                        if(r.successed){
                            //若存在，则弹出信息
                            alert(r.message)
                        }else{
                            //若失败则显示新增用户表单
                            document.getElementById("newCustomer").style.display='block'
                            alert(r.message)
                        }

                    }
                })
            })
            $("#checkBed").click(function(){
                $.ajax({
                    url:GC.bctx + "customers/checkBed",
                    type:"Get",
                    data:{bedid:$("#bedid").val()},
                    headers: {"Authorization": localStorage.getItem("token")},
                    success:function(r){
                        alert(r.message)
                    }
                })
            })
            $("#newCustomerButton").click(function(){
                $.ajax({
                    url:GC.bctx + "customers/newCustomer",
                    type:"POST",
                    data:{
                    	customername:$("#customername").val(),
                    	customersex:$("#customersex").val(),
                    	customerage:$("#customerage").val(),
                    	customertelephone:$("#customertelephone").val()
                    },
                    headers: {"Authorization": localStorage.getItem("token")},
                    success:function(r){
                        if(r.successed){
                            //新增成功,将id输出到上一层列表中
                            $("#customerid").val(r.data)
                            alert(r.message)
                            document.getElementById("newCustomer").style.display='none'
                        }else{
                            //新增失败，显示失败信息
                            alert(r.message)
                        }

                    }
                })
                return false
            })
        })
    </script>

</head>
<body>

<div class="container-fluid">
    <h1>登记入住</h1>
    <hr/>
    <form id="livein">
        <table>
            <tr>
                <td>客户id：</td>
                <td><input id="customerid" name="customerid" type="text"/></td>
                <td><input id="checkCustomer" name="checkCustomer" value="查询" type="button"/>
            </tr>
        </table>
        <table id="newCustomer"  style="display:none">
            
                    <tr>
                        <td>客户姓名：</td>
                        <td><input id="customername" name="customername" type="text"/></td>
                    </tr>
                    <tr>
                        <td>客户性别：</td>
                        <td><input id="customersex" name="customersex" type="text"/></td>
                    </tr>
                    <tr>
                        <td>客户年龄：</td>
                        <td><input id="customerage" name="customerage" type="text"/></td>
                    </tr>
                    <tr>
                        <td>客户电话：</td>
                        <td><input id="customertelephone" name="customertelephone" type="text"/></td>
                    </tr>
                    <tr>
                        <td colspan="3" class="text-center">
                            <input name="newCustomerButton" id="newCustomerButton" type="button" value="新建用户" />
                        </td>
                    </tr>
            
    </table>
    <table>
        <tr>
        <td>订单金额：</td>
        <td><input id="bookingprepayamount" name="bookingprepayamount" type="text"/></td>
        <td><span class="c-r">*</span></td>
    </tr>
    <tr>
        <td>床位id：</td>
        <td><input id="bedid" name="bedid" type="text"/></td>
        <td><input id="checkBed" name="checkBed" value="查询" type="button"/>
    </tr>
    <tr>
        <td>入住日期：</td>
        <td><input id="liveindate" name="liveindate" type="date" /></td>
        <td><span class="c-r">*</span></td>
    </tr>
    <tr>
        <td>预期离院日期：</td>
        <td><input id="expectedliveoutdate" name="expectedliveoutdate" type="date" /></td>
        <td><span class="c-r">*</span></td>
    </tr>
    <tr>
        <td colspan="3" class="text-center">
            <input type="submit" value="保存"/>
            <input type="reset" value="重置"/>
        </td>
    </tr>

    </table>
    </form>
</div>
</body>
</html>